﻿package mortal.game.view.guildNew.WareHourse
{
    import Message.Public.*;
    import __AS3__.vec.*;
    import com.mui.controls.*;
    import com.mui.display.*;
    import com.mui.events.*;
    import extend.language.*;
    import fl.data.*;
    import flash.events.*;
    import flash.utils.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.cache.guild.module.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.common.pageSelect.*;
    import mortal.game.view.guild.otherpanel.*;
    import mortal.game.view.guild.util.*;
    import mortal.game.view.pack.*;
    import mortal.game.view.wizard.*;
    import mortal.mvc.core.*;

    public class GuildNewWareHPanel extends GSprite implements GuildNewPanelI
    {
        protected var _rbg:ScaleBitmap;
        protected var _bg:ScaleBitmap;
        protected var _rtitleBg:ScaleBitmap;
        protected var _btnDealApply:GLoadingButton;
        protected var _btnResourceAllot:GLoadingButton;
        protected var _btnRecord:GButton;
        protected var _btnDonateMoney:GButton;
        protected var _btnApplyMoney:GButton;
        protected var _pageSelecter:PageSelecter;
        protected var _txtCapacity:GTextFiled;
        protected var _tip:GTextFiled;
        protected var _tabBar:GTabarNew;
        protected var _bagBtn:GButton;
        protected var _btnMyApplyList:GButton;
        private var _tabData:Array;
        protected var _rightPanel2:GuildNewWareHRight2;
        protected var _rightPanel:GuildNewWareHRight;
        protected var _rightPanel3:GuildNewWareHRight3;
        private var _bgAllApplyNum:GuildNewWareHAppNum;
        private var _list:GTileList;
        private var _goldItem:MoneyItem2;
        private var _coinItem:MoneyItem2;

        public function GuildNewWareHPanel()
        {
            this._tabData = Language.getArray(71631);
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._rbg = UIFactory.bg(417, -1, 329, 476, this, ImagesConst.PanelBg);
            this._bg = UIFactory.bg(-1, -1, 420, 475, this, ImagesConst.PanelBg);
            this._rtitleBg = UIFactory.bg(414, 37, 326, 23, this, ImagesConst.RegionTitleBg);
            this._btnDealApply = UIFactory.gLoadingButton(ImagesConst.LineOffCopyCellBtn_upSkin, 431, 8, 76, 26, this);
            this._btnDealApply.label = "" + Language.getString(71632);
            this._btnResourceAllot = UIFactory.gLoadingButton(ImagesConst.LineOffCopyCellBtn_upSkin, 519, 8, 76, 26, this);
            this._btnResourceAllot.label = "" + Language.getString(71633);
            this._btnRecord = UIFactory.gButton("" + Language.getString(71634), 647, 7, 76, 26, this);
            this._btnDonateMoney = UIFactory.gButton("" + Language.getString(71635), 295, 440, 35, 27, this);
            this._btnApplyMoney = UIFactory.gButton("" + Language.getString(71636), 260, 440, 35, 27, this);
            this._pageSelecter = UIFactory.pageSelecter(330, 442, this, PageSelecter.SingleMode);
            this._pageSelecter.setbgStlye(ImagesConst.ComboBg, GlobalStyle.textFormatBai.center());
            this._pageSelecter.maxPage = 1;
            this._pageSelecter.pageTextBoxSize = 36;
            this._pageSelecter.configEventListener(Event.CHANGE, this.onPageChange);
            this._txtCapacity = UIFactory.gTextField("", 76, 401, 120, 20, this, GlobalStyle.textFormatPutong.setColor(16777215));
            this._tip = UIFactory.gTextField("" + Language.getString(71637), 187, 401, 200, 20, this, GlobalStyle.textFormatPutong.setColor(16777215));
            this._tabBar = UIFactory.gTabBarNew(2, 9, this._tabData, 409, 200, 55, 26, this, this.onTabBarChangeHandler);
            this._bagBtn = UIFactory.gButton("" + Language.getString(71638), 340, 4, 54, 26, this);
            this._btnMyApplyList = UIFactory.gButton("" + Language.getString(71639), 431, 8, 76, 26, this);
            this._btnMyApplyList.visible = false;
            this._list = UIFactory.tileList(10, 40, 401, 360, this);
            this._list.rowHeight = 45;
            this._list.columnWidth = 45;
            this._list.rowCount = 8;
            this._list.columnCount = 9;
            this._list.setStyle("cellRenderer", GuildNewWHItemCell);
            this._goldItem = UIFactory.getUICompoment(MoneyItem2, 5, 443, this);
            this._goldItem.setBgName(ImagesConst.InputBg);
            this._goldItem.unit = EPriceUnit._EPriceUnitGold;
            this._coinItem = UIFactory.getUICompoment(MoneyItem2, 132, 443, this);
            this._coinItem.setBgName(ImagesConst.InputBg);
            this._coinItem.unit = EPriceUnit._EPriceUnitCoin;
            this._bgAllApplyNum = UIFactory.getUICompoment(GuildNewWareHAppNum, 492, 1, this);
            this._bgAllApplyNum.updateNum(0);
            this.configEventListener(DragEvent.Event_Move_In, this.itemMoveInHandler);
            Dispatcher.addEventListener(EventName.Guild_WareHouse_ClickPackItem, this.onClickPackItem);
            Cache.instance.guild.selfGuildInfo.wareHouse.isOpen = true;
            Dispatcher.dispatchEvent(new DataEvent(EventName.Guild_WareHouse_StatusChange));
            Dispatcher.dispatchEvent(new DataEvent(EventName.Guild_wareHouse_getData));
            this._btnRecord.configEventListener(MouseEvent.CLICK, this.onMouseClick);
            this._btnMyApplyList.configEventListener(MouseEvent.CLICK, this.onMouseClick);
            this._btnDealApply.configEventListener(MouseEvent.CLICK, this.onMouseClick);
            this._btnApplyMoney.configEventListener(MouseEvent.CLICK, this.onMouseClick);
            this._btnDonateMoney.configEventListener(MouseEvent.CLICK, this.onMouseClick);
            this._btnResourceAllot.configEventListener(MouseEvent.CLICK, this.onMouseClick);
            this._bagBtn.configEventListener(MouseEvent.CLICK, this.onMouseClick);
            this.update();
            this.setBtnVisible();
            this._rightPanel3 = UIFactory.getUICompoment(GuildNewWareHRight3, 416, 0, this);
            this.setVisiblePanel(3);
            Dispatcher.dispatchEvent(new DataEvent(EventName.Guild_wareHouse_getRecords));
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            GuildResourceRecordUitl.instance.clean();
            Cache.instance.guild.selfGuildInfo.wareHouse.isOpen = false;
            this._bgAllApplyNum = null;
            this._rightPanel = null;
            this._rightPanel2 = null;
            this._rightPanel3 = null;
            this._btnMyApplyList = null;
            this._goldItem = null;
            this._coinItem = null;
            this._list = null;
            this._bagBtn = null;
            this._rbg = null;
            this._bg = null;
            this._rtitleBg = null;
            this._btnDealApply = null;
            this._btnResourceAllot = null;
            this._btnRecord = null;
            this._btnDonateMoney = null;
            this._btnApplyMoney = null;
            this._pageSelecter = null;
            this._txtCapacity = null;
            this._tip = null;
            this._tabBar = null;
            super.disposeImpl(param1);
            return;
        }// end function

        private function setBtnVisible() : void
        {
            if (GuildConst.hasPermissions(GuildConst.ResourceAllot))
            {
                var _loc_1:Boolean = true;
                this._btnResourceAllot.visible = true;
                this._btnDealApply.visible = _loc_1;
            }
            else
            {
                var _loc_1:Boolean = false;
                this._btnResourceAllot.visible = false;
                this._btnDealApply.visible = _loc_1;
            }
            return;
        }// end function

        private function setVisiblePanel(param1:int) : void
        {
            var _loc_2:* = param1 == 1;
            this._rightPanel.visible = param1 == 1;
            var _loc_2:* = param1 == 2;
            this._rightPanel2.visible = param1 == 2;
            var _loc_2:* = param1 == 3;
            this._rightPanel3.visible = param1 == 3;
            return;
        }// end function

        public function onMouseClick(event:MouseEvent) : void
        {
            switch(event.target)
            {
                case this._btnRecord:
                {
                    this.setVisiblePanel(3);
                    Dispatcher.dispatchEvent(new DataEvent(EventName.Guild_wareHouse_getRecords));
                    GuildResourceRecordUitl.instance.clean();
                    break;
                }
                case this._btnMyApplyList:
                {
                    GuildWareHouseMyApplyListPanel.instance.popUp();
                    break;
                }
                case this._btnDealApply:
                {
                    if (this._rightPanel2 == null)
                    {
                        this._rightPanel2 = UIFactory.getUICompoment(GuildNewWareHRight2, 416, 0, this);
                    }
                    this.setVisiblePanel(2);
                    Dispatcher.dispatchEvent(new DataEvent(EventName.Guild_wareHouse_getAllApplyList));
                    GuildResourceRecordUitl.instance.clean();
                    break;
                }
                case this._btnApplyMoney:
                {
                    GuildWareHouseApplyMoney.instance.popUp();
                    break;
                }
                case this._btnDonateMoney:
                {
                    GuildDonateGoldPanel.instance.popUp();
                    break;
                }
                case this._btnResourceAllot:
                {
                    if (this._rightPanel == null)
                    {
                        this._rightPanel = UIFactory.getUICompoment(GuildNewWareHRight, 416, 0, this);
                    }
                    this.setVisiblePanel(1);
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GuildParticipationInfoReq));
                    GameController.pack.view.hide();
                    break;
                }
                case this._bagBtn:
                {
                    GameController.pack.view.show();
                    LayerManager.windowLayer.centerWindows(GameController.guildNew.view, GameController.pack.view);
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function onClickPackItem(event:DataEvent) : void
        {
            this.startPutInItem(event.data as ItemData);
            return;
        }// end function

        private function itemMoveInHandler(event:DragEvent) : void
        {
            var _loc_2:* = event.dragSouce as ItemData;
            var _loc_3:* = event.dragItem as BaseItem;
            var _loc_4:* = event.dropItem as BaseItem;
            if (_loc_3.parent && _loc_3.parent is PackCellRenderer && _loc_4.parent && _loc_4.parent is GuildNewWHItemCell && _loc_2.serverData.posType == EPlayerItemPosType._EPlayerItemPosTypeBag)
            {
                this.startPutInItem(_loc_2);
            }
            else
            {
                return;
            }
            return;
        }// end function

        private function startPutInItem(param1:ItemData) : void
        {
            GuildNewDonateItemPanel.instance.showPanel(param1);
            return;
        }// end function

        private function onPageChange(event:Event) : void
        {
            this.updateList();
            return;
        }// end function

        public function updateList() : void
        {
            var _loc_1:* = Cache.instance.guild.selfGuildInfo.wareHouse;
            this._list.dataProvider = this.getDataProvider();
            this._list.drawNow();
            var _loc_2:* = _loc_1.warehouseItemVec.length;
            this._pageSelecter.maxPage = Math.ceil(_loc_1.totalGrid / (this._list.rowCount * this._list.columnCount));
            this._txtCapacity.text = Language.getString(71640) + _loc_2 + "/" + _loc_1.totalGrid;
            return;
        }// end function

        public function updateApplyLabel() : void
        {
            if (GuildConst.hasPermissions(GuildConst.ManagerWareHouse))
            {
                this._bgAllApplyNum.updateNum(Cache.instance.guild.selfGuildInfo.wareHouse.allApplyNum);
            }
            return;
        }// end function

        public function updateMoneys() : void
        {
            var _loc_1:Dictionary = null;
            if (Cache.instance.guild && Cache.instance.guild.selfGuildInfo && Cache.instance.guild.selfGuildInfo.wareHouse)
            {
                _loc_1 = Cache.instance.guild.selfGuildInfo.wareHouse.moneyDic;
                if (this._coinItem && _loc_1)
                {
                    this._coinItem.value = int(_loc_1[EPriceUnit._EPriceUnitCoin]);
                    this._goldItem.value = int(_loc_1[EPriceUnit._EPriceUnitGold]);
                }
            }
            return;
        }// end function

        private function onTabBarChangeHandler(event:MuiEvent = null) : void
        {
            this._pageSelecter.currentPage = 1;
            this.updateList();
            return;
        }// end function

        private function getDataProvider() : DataProvider
        {
            var _loc_3:Vector.<SPlayerItem> = null;
            var _loc_1:* = new DataProvider();
            var _loc_2:* = Cache.instance.guild.selfGuildInfo.wareHouse;
            switch(this._tabBar.selectedIndex)
            {
                case 0:
                {
                    _loc_3 = _loc_2.warehouseItemVec;
                    break;
                }
                case 1:
                {
                    _loc_3 = _loc_2.warehouseItemVec.filter(this.isNotEquipment);
                    break;
                }
                case 2:
                {
                    _loc_3 = _loc_2.warehouseItemVec.filter(this.isEquipment);
                    break;
                }
                default:
                {
                    break;
                }
            }
            var _loc_4:* = this._list.rowCount * this._list.columnCount;
            var _loc_5:* = (this._pageSelecter.currentPage - 1) * _loc_4;
            var _loc_6:* = _loc_5 + Math.min(_loc_4, _loc_2.totalGrid - _loc_5);
            var _loc_7:* = _loc_5;
            while (_loc_7 < _loc_6)
            {
                
                if (_loc_7 < _loc_3.length)
                {
                    _loc_1.addItem(_loc_3[_loc_7]);
                }
                else
                {
                    _loc_1.addItem(new Object());
                }
                _loc_7++;
            }
            return _loc_1;
        }// end function

        private function isEquipment(param1:SPlayerItem, param2:int, param3:Vector.<SPlayerItem>) : Boolean
        {
            return ItemsUtil.isOtherEquip(new ItemData(param1.itemCode));
        }// end function

        private function isNotEquipment(param1:SPlayerItem, param2:int, param3:Vector.<SPlayerItem>) : Boolean
        {
            return !ItemsUtil.isOtherEquip(new ItemData(param1.itemCode));
        }// end function

        public function update() : void
        {
            this.updateMoneys();
            this.updateList();
            this.updateApplyLabel();
            if (GuildConst.hasPermissions(GuildConst.ManagerWareHouse))
            {
                this._btnApplyMoney.label = "" + Language.getString(71641);
                this._btnDealApply.visible = true;
                this._btnMyApplyList.visible = false;
            }
            else
            {
                this._btnApplyMoney.label = "" + Language.getString(71642);
                this._btnDealApply.visible = false;
                this._btnMyApplyList.visible = true;
            }
            return;
        }// end function

        public function updateApplyPanel() : void
        {
            if (this._rightPanel2)
            {
                this._rightPanel2.update();
            }
            return;
        }// end function

        public function updateResourcePanel(param1:Object) : void
        {
            if (this._rightPanel)
            {
                this._rightPanel.update(param1);
            }
            return;
        }// end function

        public function updateRecodePanel() : void
        {
            if (this._rightPanel3)
            {
                this._rightPanel3.update();
            }
            return;
        }// end function

        public function onallocationSuccess(param1:Object) : void
        {
            if (this._rightPanel)
            {
                this._rightPanel.onallocationSuccess(param1);
            }
            return;
        }// end function

    }
}
